/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.vrangogym.beans.backing;

import edu.chl.vrangogym.persistence.entities.User;
import edu.chl.vrangogym.persistence.entities.values.Address;
import edu.chl.vrangogym.persistence.entities.values.ContactInfo;
import edu.chl.vrangogym.persistence.impl.UserFacade;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author antonmalmquist
 */
@ManagedBean(name = "Edit")
@RequestScoped
public class EditUserDataBackingBean {

    private String firstname;
    private String lastname;
    private String mail;
    private String phone;
    private String street;
    private String zipCode;
    private String city;
    private String username;
    private String newPassword;
    private String newPassword2;
    private String oldPassword;
    
    @EJB
    UserFacade userFacade;

    /** Creates a new instance of EditUserDataBackingBean */
    public EditUserDataBackingBean() {
    }

    @PostConstruct
    private void init() {
        updateUser();
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getNewPassword2() {
        return newPassword2;
    }

    public void setNewPassword2(String newPassword2) {
        this.newPassword2 = newPassword2;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public void updateUser() {
        User sessionUser = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("Auth.user");
        if (sessionUser != null) {
            User user = userFacade.find(sessionUser.getUserName());
            ContactInfo contactInfo = user.getContactInfo();
            Address address = contactInfo.getAddress();

            firstname = contactInfo.getFirstName();
            lastname = contactInfo.getLastName();
            mail = contactInfo.getEmail();
            phone = contactInfo.getPhoneNumber();
            street = address.getStreet();
            zipCode = address.getZipCode();
            city = address.getCity();
            username = user.getUserName();
        }
    }
}
